#include"DynamicArray.h"
#include<stdio.h>
#include<string.h>
struct Student
{
    int id;
    char* name;
};
typedef struct Student Stu;


Stu* CreatStu(int id,const char *name)
{
    Stu*s=(Stu*)malloc(sizeof(Stu));
    if(s==NULL)
    {
        printf("CreateStu malloc error\n");
        exit(-1);
    }
    s->id=id;
    s->name=strdup(name);


    return s;
}
void PrintStu(void *p)
{
  Stu*s=(Stu*)p;
  printf("联系人姓名：%s  联系人电话：%d\n",s->name,s->id);

}

void PrintName(void *p)
{
    Stu *s=(Stu*)p;
    printf("学生学号：%d 学生姓名：%s\n",s->id,s->name);

}
void ClearStu(void *p)
{
    Stu*s=(Stu*)p;
    free(s->name);
    free(s);
    printf("清理学生!\n");
}
bool IsNameEqual(void *p1,void*p2)
{
    Stu *s=(Stu*)p1;
    const char *name=(const char*)p2;
    return strcmp(s->name,name)==0;
}
bool IsIdEqual(void *p1,void *p2)
{
     Stu *s=(Stu*)p1;
     long id=(long)p2;
     return s->id==id;
}
void PrintClass(void*p)
{
    DArray *a=(DArray*)p;
    PrintDArray(a,PrintStu);

}
void ClearClass(void*p)
{
     DArray *a=(DArray*)p;
     ClearDArray(a,ClearStu);
}
void Split(DArray *a,const char *src,const char *delim)
{  
     char *temp=strdup(src);
     char *dest=strtok(temp,delim);
     while(dest!=NULL)
     {
        InsertDArray(a,strdup(dest));
        dest=strtok(NULL,delim);
     }

     free(temp);
}


void PrintString(void *p)
{
    printf("%s\n",(char*)p);
}
bool BiggerID(void *p1,void*p2)
{
    Stu *s1=(Stu*)p1;
    Stu *s2=(Stu*)p2;
    return s1->id>s2->id;
}

bool SmallerID(void *p1,void*p2)
{
    Stu *s1=(Stu*)p1;
    Stu *s2=(Stu*)p2;
    return s1->id<s2->id;
}
bool BiggerName(void *p1,void*p2)
{
    Stu *s1=(Stu*)p1;
    Stu *s2=(Stu*)p2;
   return strcmp(s1->name,s2->name)>0;
}
bool SmallerName(void *p1,void*p2)
{
    Stu *s1=(Stu *)p1;
    Stu *s2=(Stu *)p2;
   return strcmp(s1->name,s2->name)<0;
}
int IDcmp(void *p1,void *p2)
{
     Stu *s1=(Stu *)p1;
     
     return s1->id-(long)p2;
}
int Namecmp(void *p1,void *p2)
{
   
     Stu *s1=(Stu *)p1;
     return strcmp(s1->name,(const char*)p2);
}

int main()
{
    DArray *a=InitDArray();
    InsertDArray(a,CreatStu(3,"zhangsan"));
    InsertDArray(a,CreatStu(1,"Lisi"));
    InsertDArray(a,CreatStu(2,"wangwu"));
    FastSort(a,BiggerID);
     void *s=BinarySearch(a,"Lisi",Namecmp);
     if(s)
     {
        printf("找到了：");
        PrintStu(s);
     }
     else
     {
    
        printf("没找到");
     }
     PrintDArray(a,PrintStu);

    // void *s=FindByElement(a,"Lisi",IsNameEqual);
    // void *s=FindByElement(a,(void*)3,IsNameEqual);
    // if(s)
    // {
    //     PrintStu(s);
    // }
    // else
    // {
    //     printf("没找到！\n");
    // }
    // PrintDArray(a,PrintStu);
    // ClearDArray(a,ClearStu);

    // DArray *class1=InitDArray();
    // InsertDArray(class1,CreatStu(1,"zhangsan"));
    // InsertDArray(class1,CreatStu(2,"Lisi"));
    // InsertDArray(class1,CreatStu(3,"wangwu"));



    // DArray*class2=InitDArray();
    // InsertDArray(class2,CreatStu(4,"zhangliu"));
    // InsertDArray(class2,CreatStu(5,"Liqi"));
    // InsertDArray(class2,CreatStu(6,"wangba"));

    // DArray*school=InitDArray();
    // InsertDArray(school,class1);
    // InsertDArray(school,class2);

    // PrintDArray(school,PrintClass);
    // ClearDArray(school,ClearClass);


    // 字符串切割   字符串列表
    // DArray *a=InitDArray();
    // Split(a,"zhangsan,lisi,wangwu",",");
    // PrintDArray(a,PrintString);
    // ClearDArray(a,free);//只有一串字符串，因为free的格式是符合的，可以直接用free

    return 0;
}